<template>
  <div>
    <div class="logo_container">
      <img src="../assets/img/login.jpg" width="100%" height="100%"/>
      <h2 class="logo">ThunderSoft.UA</h2>
      <h2 class="name">Ukraine Finance System</h2>
    </div>
    <div class="login_box">
      <div style="height: 100px;width: 100%;background-color: rgba(48, 65, 96, 0.8);color: #FFFFFF;">
        <h1 style="padding-top: 30px;padding-left: 80px;">Welcome to Login</h1>
      </div>
    <!-- 登录表单区 -->
      <el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-ren">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input :type="isShow?'text':'password'" v-model="loginForm.password" prefix-icon="iconfont icon-suo">
          <i @click="show()" style="width:30px;height:30px" slot="suffix" :class="isShow?'el-icon-open':'el-icon-turn-off'"></i></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">Login in</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { apiLogin } from '../request/api'// 导入我们的api接口
export default {
  data () {
    return {
      isShow:false,
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: 'please enter username', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'please enter password', trigger: 'blur' },
          // { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  mounted () {
    // 绑定回车事件
    window.addEventListener('keydown', this.keyDown)
  },
  destroyed () {
    // 移除回车事件
    window.removeEventListener('keydown', this.keyDown, false)
  },
  methods: {
    changeText() {
      this.isEdit = true
    },
    show(){
      this.isShow?this.isShow=false:this.isShow=true
    },
    // reset () {
    //   this.$refs.loginFormRef.resetFields()
    // },
    keyDown (e) {
      // 如果是回车则执行登录方法
      if (e.keyCode === 13) {
        this.login()
      }
    },
    // 登录
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        var userName=this.loginForm.username.trim()
        apiLogin({
          username: userName,
          password:this.loginForm.password
        }).then(res => {
          if (res.code == 200) {
            this.$store.dispatch('setUserInfo', res)
            sessionStorage.setItem('token', res.data.token)
            sessionStorage.setItem('level',res.data.level)
            sessionStorage.setItem('userName',res.data.username)
            var reg = /\?/g
            var href = sessionStorage.getItem('href')
            if(reg.test(href)){
              href = (href || "").split('/')[5]
              href = (href || "").split("?")[0]
              this.$router.push(`/reimbursementDetail/${href}`)
            }else {
              href = (href || "").split('/')
              if(href[4] == 'payAdminDetail'){
                this.$router.push(`/payAdminDetail/${href[5]}`)
              }else if(href[4] == 'paymentDetail'){
                this.$router.push(`/paymentDetail/${href[5]}`)
              }else {
                this.$router.push('/test/myReimbursement')
              }
            }
          }else if(res.code == 304){
            this.$message.error('The user is not in the system, please contact the administrator to synchronize the personnel information!')
          }else {
            this.$message.error('Account or password error!')
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.logo_container {
  width: 100%;
  height: 100%;
  top:0;
  z-index: -1;
  position: absolute;
  .logo {
      position: absolute;
      font-size: 100px;
      color: #FFFFFF;
      left: 4%;
      top: 12%;
  }
  .name {
    position: absolute;
    font-size: 40px;
    color: #FFFFFF;
    left: 5%;
    top: 28%;
 }
}
.login_box{
width:400px;
height: 350px;
background-color: #fff;
border-radius: 5px;
position: absolute;
left:25%;
top: 60%;
transform: translate(-50%,-50%);
}
.btn{
width: 100%;
}
.login_container .el-button{
width: 200px;
margin-left:80px;
}
.login_form{
position: absolute;
bottom: 0;
width: 100%;
padding: 0 20px;
box-sizing: border-box;
}
</style>
